import React, { Component } from 'react'
import './index.css'

export default class Item extends Component {
  state = {mouse:false}
  mouse=(flag)=>{
    return ()=>{
      this.setState({mouse:flag})
    }
  }
  handleChange=(id)=>{
    return (event)=>{
      // console.log(id,event.target.checked);
      this.props.updateTodo(id,event.target.checked)
    }
  }
  handleDelete=(id)=>{
    if(window.confirm('确定删除吗？')){
      this.props.deleteTodo(id)
    }
  }
  render() {
    const {id,name,done} = this.props
    const {mouse} = this.state
    return (
      <div>
        <li style={{backgroundColor:mouse? '#ddd':'white'}} onMouseEnter={this.mouse(true)} onMouseLeave={this.mouse(false)}>
          <label>
              <input type="checkbox" checked={done} onChange={this.handleChange(id)}/>
              <span>{name}</span>
          </label>
          <button className="btn btn-danger" style={{display:mouse?'block':'none'}} onClick={()=>{this.handleDelete(id)}}>删除</button>
        </li>
      </div>
    )
  }
}
